package com.app.conrtoller;

import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.hsy.SendMessage;
import com.app.domain.ldpf.YjSingle;
import com.app.domain.wxgd.WxgdResponseResult;
import com.app.domain.wxjjeb.UploadDataYJ;
import com.app.domain.wxjjeb.WxjjebResponseResult;
import com.app.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 无锡交建二标
 */
@Slf4j
@RestController
@RequestMapping("/wxjjeb")
public class WxjjebUploadController {
    @Value("${yj.wxjjeb.serviceHost}")
    private String serviceHost;

    @Value("${yj.wxjjeb.token}")
    private String token;
    @Value("${yj.wxjjeb.equNum}")
    private String equNum;
    @Value("${yj.wxjjeb.projectId}")
    private String projectId;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    WxjjebService uploadService;
    @Autowired
    SendMessageService sendMessageService;

    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
        try {
            for (String id : ids) {
                YjData yjData = yjDataService.findOne(id);
                UploadParameter uploadParameter = uploadParameterService.findOne("43");
                String uuid = sendMessageService.ConditionQuery(id);
                if (StringUtils.isEmpty(uuid)) {
                    UploadDataYJ yjSingle = uploadService.getData(yjData, uploadParameter, uuid, equNum, projectId);
                    WxjjebResponseResult sqResponseResult = uploadService.sendMessage(serviceHost, yjSingle, null, token);
                    if (sqResponseResult != null && "200".equals(sqResponseResult.getCode())) {
                        //主表上传成功
                        SendMessage sendMessage = new SendMessage();
                        sendMessage.setSbbh(yjData.getDevice_code());
                        sendMessage.setLh(yjData.getBridge_code());
                        sendMessage.setKh(yjData.getDuct_code());
                        sendMessage.setBusi_type("mudjack");
                        sendMessage.setUpload_time(new Date());
                        sendMessage.setUuid(yjSingle.getUuId());
                        sendMessage.setStatus("0");
                        sendMessageService.insert(sendMessage);
                        updateState("1", String.valueOf(yjData.getId()));
                        succTotal++;
                    } else {
                        updateState("0", String.valueOf(yjData.getId()));
                        failTotal++;
                    }
                } else {
                    UploadDataYJ yjSingle = uploadService.getData(yjData, uploadParameter, uuid, equNum, projectId);
                    WxjjebResponseResult sqResponseResult = uploadService.sendMessage(serviceHost, yjSingle, null, token);
                    if (sqResponseResult != null && "200".equals(sqResponseResult.getCode())) {
                        updateState("1", String.valueOf(yjData.getId()));
                        succTotal++;
                    } else {
                        updateState("0", String.valueOf(yjData.getId()));
                        failTotal++;
                    }
                }
            }
        } catch (Exception e) {
        log.error("数据上传失败",e);
            externalResponse = new ExternalResponse(0, "未知错误,数据上传失败", "");
            return externalResponse;
        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }


    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }
}